import { defineStore } from "pinia";
import { reactive } from "vue";

interface NoticeItem {
	title?: string;
	read: boolean;
	message: any;
	time: string;
}

export const useNoticeStore = defineStore("notice", () => {
	const state = reactive({
		notices: [] as NoticeItem[],
	});

	const addNotice = (notice: NoticeItem) => {
		state.notices.push(notice);
	};

	const removeNotice = (notice: NoticeItem) => {
		state.notices.splice(state.notices.indexOf(notice), 1);
	};

	//实现全部已读
	const readAll = () => {
		state.notices.forEach((item) => {
			item.read = true;
		});
	};

	const clearNotice = () => {
		state.notices = [];
	};

	//刷新数据
	const refreshData = () => {
		state.notices.push({
			title: "",
			read: false,
			message: "",
			time: new Date().toLocaleString(),
		});
	};

	return {
		state,
		addNotice,
		removeNotice,
		readAll,
		clearNotice,
		refreshData,
	};
});

export default useNoticeStore;
